/**
 * Title : jFamilyTree applet
 * Description: jFamilyTree applet
 * About: Designed and rewritten by Andreas Lundgren, based on the Treebolic application suite
 * Copyright:	(c) 2001-2008
 * Terms of use:see license agreement at http://treebolic.sourceforge.net/en/license.htm
 * Author: Bernard Bou
 * Company: bsys
 */
package jFamilyTree.applet;

import java.applet.AppletContext;
import java.awt.Component;
import java.net.MalformedURLException;
import java.net.URL;

import javax.swing.JApplet;
import javax.swing.UIManager;

import jFamilyTree.IContext;
import jFamilyTree.IWidget;
import jFamilyTree.Widget;

/**
 * Applet
 * 
 * @author Bernard Bou
 */
public class jFamilyTree extends JApplet implements IContext
{
	private static final long serialVersionUID = 1L;

	/**
	 * Component widget
	 */
	private IWidget theWidget;

	// C O N S T R U C T O R

	/**
	 * Constructor
	 */
	public jFamilyTree()
	{
		UIManager.put("swing.boldMetal", false);
		theWidget = new Widget(this);
		add((Component) theWidget);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.applet.Applet#init()
	 */
	public void init()
	{
		theWidget.init();
	}

	// V E R S I O N (for Javascript)

	/**
	 * Get version (wrapper)
	 */
	public String getVersion()
	{
		return theWidget.getVersion();
	}

	// S E A R C H (for Javascript)

	/**
	 * Match node against string (wrapper)
	 * 
	 * @param thisTargetString
	 *        string to search for
	 * @param thisScopeString
	 *        scope ("label", "content", "link", "id")
	 * @param thisModeString
	 *        mode ("equals", "startswith", "includes")
	 * @return node id
	 */
	public String match(String thisTargetString, String thisScopeString, String thisModeString)
	{
		String thisResult = theWidget.match(thisTargetString, thisScopeString, thisModeString);
		return thisResult;
	}

	// F O C U S (for Javascript)

	/**
	 * Focus
	 * 
	 * @param thisNodeId
	 *        (wrapper) node id to get focus
	 */
	public void focus(String thisNodeId)
	{
		theWidget.focus(thisNodeId);
	}

	// L I N K (for Javascript)

	/**
	 * Link to Url (wrapper)
	 * 
	 * @param thisUrlString
	 *        link url string
	 * @param thisUrlTarget
	 *        link target string
	 */
	public void link(String thisUrlString, String thisUrlTarget)
	{
		theWidget.link(thisUrlString, thisUrlTarget);
	}

	// P A R A M E T E R S

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.applet.Applet#getAppletInfo()
	 */
	public String getAppletInfo()
	{
		return "Title: jFamilyTree\nAuthor: Bernard Bou";
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.applet.Applet#getParameterInfo()
	 */
	public String[][] getParameterInfo()
	{
		String[][] theinfo = { { "doc", "string", "data source" }, { "xml", "string", "data source" }, { "base", "string", "data base" }, { "images", "string", "image base" }, { "relativeto", "string", "images base relative to (docbase|base)" } };
		return theinfo;
	}

	// C O N T E X T

	/*
	 * (non-Javadoc)
	 * 
	 * @see jFamilyTree.component.Context#linkTo(java.lang.String)
	 */
	public boolean linkTo(String thisHref, String thisTarget)
	{
		System.out.println("Applet linking to " + thisHref + " - " + thisTarget);
		try
		{
			// make URL
			URL thisURL;
			try
			{
				thisURL = new URL(thisHref);
			}
			catch (MalformedURLException e)
			{
				thisURL = new URL(getCodeBase(), thisHref);
			}

			// jump link
			System.out.println("URL is " + thisURL);
			AppletContext thisAppletContext = getAppletContext();
			if (thisTarget == null)
				thisAppletContext.showDocument(thisURL);
			else
				thisAppletContext.showDocument(thisURL, thisTarget);
		}
		catch (Exception thisException)
		{
			System.err.println("Can't link to " + thisHref + " - " + thisException);
			thisException.printStackTrace();
		}
		return true;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see jFamilyTree.Context#getBase()
	 */
	public URL getBase()
	{
		URL thisDocumentBase = getDocumentBase();
		String thisBase = getParameter("base");
		if (thisBase != null && !thisBase.isEmpty())
		{
			// 1-use 'base' parameter as full-fledged url
			try
			{
				return new URL(thisBase);
			}
			catch (MalformedURLException e)
			{
			}

			// 2-use 'base' parameter as relative to document base
			if (!thisBase.endsWith("/"))
				thisBase += "/";
			try
			{
				return new URL(thisDocumentBase, thisBase);
			}
			catch (MalformedURLException e)
			{
			}
		}

		// 3-default to document base
		return thisDocumentBase;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see jFamilyTree.Context#getImagesBase()
	 */
	public URL getImagesBase()
	{
		URL thisDocumentBase = null;
		URL thisBase = null;
		String thisImageBase = getParameter("images");
		boolean relativeToBase = !"docbase".equals(getParameter("relativeto"));
		if (thisImageBase != null && !thisImageBase.isEmpty())
		{
			// 1-use 'images' parameter as full-fledged url
			try
			{
				return new URL(thisImageBase);
			}
			catch (MalformedURLException e)
			{
			}

			if (!thisImageBase.endsWith("/"))
				thisImageBase += "/";

			if (relativeToBase)
			{
				// 2-use 'images' parameter as relative to base
				thisBase = getBase();
				try
				{
					return new URL(thisBase, thisImageBase);
				}
				catch (MalformedURLException thisException)
				{
				}
			}
			else
			{
				// 2-use 'images' parameter as relative to document base
				thisDocumentBase = getDocumentBase();
				try
				{
					return new URL(thisDocumentBase, thisImageBase);
				}
				catch (MalformedURLException thisException)
				{
				}
			}
		}

		if (relativeToBase)
		{
			// 3-use set 'images/' path as relative to base
			if (thisBase == null)
				thisBase = getBase();
			try
			{
				return new URL(thisBase, "images/");
			}
			catch (MalformedURLException thisException)
			{
			}
		}
		else
		{
			// 3-use set 'images/' path as relative to document base
			if (thisDocumentBase == null)
				thisDocumentBase = getDocumentBase();
			try
			{
				return new URL(thisDocumentBase, "images/");
			}
			catch (MalformedURLException thisException)
			{
			}
		}
		return null;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see jFamilyTree.Context#getHtmlDocumentBase()
	 */
	public URL getHtmlDocumentBase()
	{
		return getDocumentBase();
	}
}